
//print
$(".btn-print").click(function(){
	$(".nav-tabs").hide();
	window.print();
	$(".nav-tabs").show();
});
//按周 统计  
function callbackByWeek(target, retdata, datetime){
	if(callbackByDateTime(target, retdata, datetime+"所在周")){//先画 总统计 信息
		//按周显示统计信息
		var data = retdata.data3;
		data[0]= ["周一","周二","周三","周四","周五","周六","周日"]; 
		drawMapByMonth( $(target).find(".map>div[name=map3]"), data, datetime+"所在周", "按周统计");
	}
}
//按年 统计  
function callbackByYear(target, retdata, datetime){
	if(callbackByDateTime(target, retdata, datetime+"年  ")){
	//按月显示统计信息
		var data = retdata.data1;
		for (var index in data[0]) {
			data[0][index] = data[0][index] + "月";
		}
		drawMapByMonth( $(target).find(".map>div[name=map3]"), data, datetime+ "年   订单统计", "");
	}
}

//按月 统计  
function callbackByMonth(target, retdata, datetime){
	if(callbackByDateTime(target, retdata, datetime)){//先画 总统计 信息 
		//按天显示统计信息
		var data = retdata.data2;
		for(var index in data[0]){
			data[0][index] = data[0][index] + "号";
		}
		drawMapByMonth( $(target).find(".map>div[name=map3]"), data, datetime+"", "按月统计");
	}
}
//按月 统计 map
function drawMapByMonth(obj, data, title, name){
	obj.highcharts({
		 chart: {
	            type: 'spline',
	            zoomType: 'x'
	        },
	        title: {
	            text: title
	        },
	        subtitle: {
	            text: name
	        },
	        xAxis: {
        	   title: {
	              text: '日期'
	           },
	           minTickInterval: 2,
	           categories: data[0],
	           labels: {
	                formatter: function() {
	                    return this.value ;
	                }
	            }
	        },
	        yAxis: {
	            title: {
	                text: '数量(条)'
	            },
	            labels: {
	                formatter: function() {
	                    return this.value +'条';
	                }
	            }
	        },
	        exporting: {
	        	filename: title
	        },
	        credits:{
	        	enabled: true,
	        	href: 'http://ipark.teleframe.cn',
	        	text: 'ipark.teleframe.cn',
	        },
	        tooltip: {
	            crosshairs: true,
	            shared: true,  
	            pointFormat: '{series.name}: <b>{point.y} 条  </b>'
	        },
	        plotOptions: {
	            spline: {
	                marker: {
	                    radius: 4,
	                    lineColor: '#666666',
	                    lineWidth: 1
	                }
	            }
	        },
	        series: [{
		         name: '失败总量',
	             data: data[1]
	        },{
	        	 name: '待支付总量',
		         data: data[2]
	        },{
	        	 name: '逃费总量',
	             data: data[3]
	        },{
	        	 name: '成交总量',
		         data: data[4]
	        }]
    });
}

//按天统计
function callbackByDay(target, retdata, datetime){
	callbackByDateTime(target,retdata,datetime);
}

//按天统计
function callbackByDateTime(target, retdata, datetime){
	var payInOut = retdata.payInOut;//收支统计
	if(payInOut.length==0){
		alert("暂无数据");
		return false;
	}else if(payInOut.length <= 7){
		var data = new Array();
		for(var par in payInOut){
			var d = new Array();
			if(payInOut[par].status==4){
				d.push("待支付总量");
				d.push( NonNegative(payInOut[par].countOrder) );
				data.push(d);
			}else if(payInOut[par].status==-1){
				d.push("失败总量");
				d.push( NonNegative(payInOut[par].countOrder) );
				data.push(d);
			}else if(payInOut[par].status==5){
				d.push("逃费总量");
				d.push( NonNegative(payInOut[par].countOrder) );
				data.push(d);
			}else if(payInOut[par].status==20){
				d.push("成交总量");
				d.push( NonNegative(payInOut[par].countOrder) );
				data.push(d);
			}
			console.log( d.join(",") );
		}
		drawMapByDay( $(target).find(".map>div[name=map1]"), data, datetime+"订单统计", "按天统计");
	}else{
		alert("订单统计信息异常");
		return false;
	}
	var payStat = retdata.payStat;//失败统计饼图
	if(payStat.length <= 4){
		var data1 = new Array();
		for(var par in payStat){
			var d = new Array();
			if(payStat[par].status==-1){
				d.push("管理员确认无法预订");
				d.push( NonNegative(payStat[par].countOrder));
				data1.push(d);
			}else if(payStat[par].status==-2){
				d.push("超时未确认定的订单");
				d.push( NonNegative(payStat[par].countOrder));
				data1.push(d);
			}else if(payStat[par].status==-3){
				d.push("超时未支付的订单");
				d.push( NonNegative(payStat[par].countOrder));
				data1.push(d);
			}else if(payStat[par].status==0){
				d.push("订单已取消");
				d.push( NonNegative(payStat[par].countOrder));
				data1.push(d);
			}
		}
		drawMapByDay( $(target).find(".map>div[name=map2]"), data1, datetime+"按失败订单类型统计", "");
	}else{
		alert("按失败订单类型统计出错");
	}
	return true;
}
//按天统计map
function drawMapByDay(obj, data, title, name){
	obj.highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: true
        },
        title: {
            text: title
        },
        tooltip: {
    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        exporting: {
        	filename: title
        },
        credits:{
        	enabled: true,
        	href: 'http://ipark.teleframe.cn',
        	text: 'ipark.teleframe.cn',
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.y} 条'
                },
                showInLegend: true
            }
        },
        series: [{
            type: 'pie',
            name: name,
            data: data
        }]
    });
}

$("div.container>div.row input[type='button']").click(function(){
	var stattype = $(this).attr("stattype");
	var stattypefor = $(this).attr("for");
	var callbackMap = $(this).attr("callback");
	var target = $(this).parent().parent();
	var datetime = $("#"+stattypefor).val();
	if(window.console){
		console.log("stattype="+stattype+" stattypefor="+stattypefor);
	}
	var data={
			stattype: stattype,
			datetime: datetime
		};
 
	startMask();//等待提示开启

	$.ajax({
		type: 'POST',
		url: "OrderStatisticsAction?method=query&querytype=json",
		data: data,
		success: function(retdata){
			
			endMask();//等待提示关闭
			
			console.log(retdata);
			
			if(retdata.error==-100){
				alert( retdata.msg );
				window.top.location = retdata.url;
			}else{
				if(retdata.error==0){
					window[callbackMap]( target, retdata, datetime );//按天统计
				}else{
					alert( retdata.msg );
				}
			}
	  	},
		dataType: "json",
		error : function(XMLHttpRequest, textStatus, errorThrown){
			alert( XMLHttpRequest.responseText);
			endMask();//等待提示关闭
			$("#errorFiled").html( XMLHttpRequest.responseText );
			$("#errorFiled").show();
           
		}
	});
});

function switchtab( tabtype ){
	$("div.container>div.row").hide();
	$("div.container>div.row[type='"+tabtype+"']").show() ;
	if(window.console){
		//console.log("switchtab @ "+tabtype);
	}
}

$("ul.nav-tabs li").mouseover(function(){
	var tab = $(this);
	var nav = tab.parent();
	$(nav).children("li").removeClass("active");
	tab.addClass("active");
	var type = $(this).attr("type");
	switchtab(type);
	return true;
});

